package com.jinshan.edu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinshan.edu.entity.EduCourse;
import com.jinshan.edu.service.EduCourseService;
import com.jinshan.request.CourseInfoVO;
import com.jinshan.response.EduCourseInfoVO;
import com.jinshan.response.RetVal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Jinshan
 * @since 2020-06-26
 */
@RestController
@RequestMapping("/edu/course")
@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;

    // 保存课程
    @PostMapping("/saveCourse")
    public RetVal saveCourse(@RequestBody CourseInfoVO courseInfoVO) {
        boolean flag = courseService.saveCourse(courseInfoVO);
        if (flag) {
            return RetVal.success();
        } else {
            return RetVal.error();
        }
    }

    // 更新课程
    @PutMapping("/updateCourse")
    public RetVal updateCourse(@RequestBody CourseInfoVO courseInfoVO) {
        boolean flag = courseService.updateCourse(courseInfoVO);
        if (flag) {
            return RetVal.success();
        } else {
            return RetVal.error();
        }
    }

    // 通过id获取课程信息
    @GetMapping("/{id}")
    public RetVal getCourseById(@PathVariable(value = "id") String courseId) {
        CourseInfoVO courseInfoVO = courseService.getCourseById(courseId);
        return RetVal.success().data("courseInfoVO", courseInfoVO);
    }

    @GetMapping("/getAllCourse")
    public RetVal getAllCourse() {
        List<EduCourse> allCourses = courseService.getAllCourse();
        return RetVal.success().data("allCourses", allCourses);
    }

    @GetMapping("/{pageNum}/{pageSize}")
    public RetVal getCourseByCondition(
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize,
            @RequestParam("title") String title,
            @RequestParam("status") String status) {
        Page<EduCourse> coursePage = new Page<>(pageNum, pageSize);
        courseService.getCourseByCondition(coursePage, title, status);
        List<EduCourse> courses = coursePage.getRecords();
        long total = coursePage.getTotal();
        return RetVal.success().data("courses", courses).data("total", total);
    }

    @GetMapping("/getCourseInfo/{courseId}")
    public RetVal getCourseInfoById(@PathVariable String courseId) {
        EduCourseInfoVO courseInfoVO = courseService.getCourseInfoById(courseId);
        return RetVal.success().data("courseInfoVO", courseInfoVO);
    }

    // 根据课程id更新课程的发布状态
    @PutMapping("/{courseId}")
    public RetVal updateCourseStatus(@PathVariable String courseId) {
        courseService.updateCourseStatus(courseId);
        return RetVal.success();
    }

    @DeleteMapping("{courseId}")
    public RetVal deleteCourseById(@PathVariable String courseId) {
        // 通过courseId删除课程, 需要删除课程, 和课程关联的subject description 和 section
        // 删除section的同时, 需要删除section关联的视频,
        boolean flag = courseService.deleteCourseById(courseId);
        if (flag) {
            return RetVal.success().message("删除课程成功");
        } else {
            return RetVal.error().message("删除课程失败");
        }
    }
}

